﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using JupiterLibrary;

namespace KinnectInput
{
    public enum HandCalibration
    {
        Waiting,
        Detected,
        MaxX,
        MaxZ,
        MaxY,
        Succeded
    }
    public class Hand
    {
        
        public HandCalibration calibrationState { get; set; }
        
        public Vector3 Position { set; get; }
        public Vector3 ShoulderPositon { set; get; }
        public float maxX { get;  private set; }
        public float maxY { get; private set; }
        public float maxZ { get; private set; }
        public Ring waitingRing;
        public int id { set; get; }
        bool firstTimeWaiting;
        public Hand()
        {
            //Ponemos la mano a waiting
            calibrationState = HandCalibration.Waiting;
            //inicializamos el circulo
            waitingRing = new Ring(0, 0, 100,10);
            waitingRing.Position = new Vector3(200, 100, 0);
            
        }
        public void Update()
        {
            if (firstTimeWaiting)
            {
                firstTimeWaiting = false;
            }
            if (calibrationState == HandCalibration.Detected)
            {
                calibrationState = HandCalibration.Succeded;

            }
            if (calibrationState == HandCalibration.MaxX)
            {
                maxX = EvalAbsoluteValue(maxX, Position.X);
                if (waitingRing.EndAngle > 360)
                {
                    calibrationState = HandCalibration.MaxY;
                    ResetWaitingCircle();
                }
            }
            if (calibrationState == HandCalibration.MaxY)
            {
                maxY = EvalAbsoluteValue(maxY, Position.Y);
                if (waitingRing.EndAngle > 360)
                {
                    calibrationState = HandCalibration.MaxZ;
                    ResetWaitingCircle();
                }
            }
            if (calibrationState == HandCalibration.MaxZ)
            {
                maxZ = EvalAbsoluteValue(maxY, Position.Z);
                if (waitingRing.EndAngle > 360)
                {
                    calibrationState = HandCalibration.Succeded;
                    ResetWaitingCircle();
                }
            }

            //update del ring 
            if (calibrationState != HandCalibration.Succeded)
            {
                waitingRing.EndAngle += Jupiter.AtXSeconds(5, 350);
                if (waitingRing.EndAngle > 360) waitingRing.EndAngle = 361;
                waitingRing.Update();
            }

        }
        public void Draw()
        {
            if (calibrationState != HandCalibration.Succeded)
                waitingRing.Draw();
        }
        private float EvalAbsoluteValue(float oldF,float newF)
        {
            //me importa el valor sin simbolo
            newF = Math.Abs(newF);

            if (newF > oldF)
            {
                return newF;
            }
            else
            {
                return oldF;
            }
            
        }
        private void ResetWaitingCircle()
        {
            waitingRing.EndAngle = 0;
            firstTimeWaiting = true;
        }
    }
}
